package org.spring.flower.resume.message;

import ch.qos.logback.classic.gaffer.PropertyUtil;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;

import java.util.HashMap;
import java.util.Map;

@SpringBootConfiguration
public class RabbitMqDeadLetterConfig {

    @Value("${mq.dead.letter.exchange}")
    public static String DL_EXCHANGE = ;
    @Value("${mq.dead.letter.routeKey}")
    public static String DL_ROUTE_KEY;
    @Value("${mq.dead.letter.redirectKey}")
    public static String DL_REDIRECT_KEY;
    @Value("${mq.dead.letter.queue}")
    public static String DL_QUEUE;
    @Value("${mq.dead.letter.redirectQueue}")
    public static String DL_REDIRECT_QUEUE;

    @Bean
    public Exchange deadLetterExchange() {
        return ExchangeBuilder.directExchange(DL_EXCHANGE).durable(true).build();
    }

    @Bean
    public Queue deadLetterQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", DL_EXCHANGE);
        args.put("x-dead-letter-routing-key", DL_ROUTE_KEY);
        return QueueBuilder.durable(DL_QUEUE).withArguments(args).build();
    }

    @Bean("redirectQueue")
    public Queue redirectQueue() {
        return QueueBuilder.durable(DL_REDIRECT_QUEUE).build();
    }


    /**
     * 死信队列绑定到死信交换器上.
     *
     * @return the binding
     */
    @Bean
    public Binding deadLetterBinding() {
        return new Binding(DL_QUEUE, Binding.DestinationType.QUEUE, DL_EXCHANGE, DL_ROUTE_KEY, null);
    }


    /**
     * 将重定向队列通过routingKey(TKEY_R)绑定到死信队列的
     * Exchange上
     *
     * @return the binding
     */

    @Bean
    public Binding redirectBinding() {
        return new Binding(DL_REDIRECT_QUEUE, Binding.DestinationType.QUEUE, DL_EXCHANGE, DL_REDIRECT_KEY, null);
    }


}
